//
//  SleepChartView.m
//  Lightthings
//
//  Created by 王岩 on 2025/2/13.
//

#import "SleepChartView.h"
#import <AAChartKit/AAChartKit.h>

@interface SleepChartView ()
/* <#note#> */
@property (nonatomic, strong) UIImageView * iconView;
@property (nonatomic, strong) UILabel * titleLab;
@property (nonatomic, strong) AAChartView * aaChartView;
@end

@implementation SleepChartView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = KLColor(@"#44598D");
        self.layer.cornerRadius = 10;
        self.layer.masksToBounds = YES;
        
        _iconView = [JYInitializer jy_initImageViewWithImage:U_imageNamed(@"心率") on:self layout:^(MASConstraintMaker * _Nonnull make) {
            kMakeLV(15);
            kMakeTV(10);
            kMakeWHV(30, 30);
        }];
        _iconView.backgroundColor = [UIColor clearColor];
        
        _titleLab = [JYInitializer jy_initLabelWithText:@"-" textColor:KLColor(@"#FFFFFF") font:U_font(14) bgColor:[UIColor clearColor] on:self layout:^(MASConstraintMaker * _Nonnull make) {
            kMakeLV(50);
            kMakeCenterY(self.iconView);
        }];
        
        CGFloat chartViewWidth  = kScreenWidth - 30;
        CGFloat chartViewHeight = 250;
        _aaChartView = [[AAChartView alloc]init];
        _aaChartView.frame = CGRectMake(0, 50, chartViewWidth, chartViewHeight);
        _aaChartView.backgroundColor = [UIColor clearColor];
        [self addSubview:_aaChartView];
        self.aaChartView.scrollEnabled = NO;
        
    
    }
    return self;
}

- (void)setTitle:(NSString *)title {
    _title = title;
    
    self.titleLab.text = title;
    
    AAChartModel *aaChartModel = AAChartModel.new
    .chartTypeSet(AAChartTypeSpline)
    .backgroundColorSet(@"#44598D")
    .tooltipEnabledSet(true)
    .yAxisGridLineStyleSet(AALineStyle.new
                           .widthSet(@0)
                           .colorSet(@"#FFFFFF"))
    .yAxisLabelsStyleSet(AAStyle.new
                         .colorSet(@"#FFFFFF")
                         )
    .borderRadiusSet(@2)
    .stackingSet(AAChartStackingTypeNormal)
    .dataLabelsEnabledSet(false)
    .zoomTypeSet(AAChartZoomTypeX)
    .markerRadiusSet(@2)
    .legendEnabledSet(false)
    .xAxisLabelsEnabledSet(false)
    .markerSymbolStyleSet(AAChartSymbolStyleTypeBorderBlank)
    .seriesSet(@[AASeriesElement.new
               .nameSet(title)
               .dataSet(@[@0,@0,@0,@0,@0,@0,@0,@0,@0,@0,@0])
               .colorSet((id)[AAGradientColor deepSeaColor])]);
    
    AAOptions *aaOptions = aaChartModel.aa_toAAOptions;
   
    [self.aaChartView aa_drawChartWithOptions:aaOptions];
}

- (void)setIcon:(NSString *)icon {
    self.iconView.image = U_imageNamed(icon);
}

- (void)updateLine:(NSString *)data {
    
//    NSLog(@"心率数据：%@", data);
    long heartValue = strtoll([[NSString stringWithFormat:@"0x%@",data] UTF8String], 0, 16);
    
    AADataElement * options0 = AADataElement.new
    .ySet(@(heartValue))
    .dataLabelsSet(AADataLabels.new
                   .colorSet(@"deepskyblue")
//                   .formatSet(@"{y:.2f} 英镑")
                   )
    .markerSet(AAMarker.new
               .radiusSet(@2)//曲线连接点半径
               .symbolSet(AAChartSymbolTypeCircle)//曲线点类型："circle", "square", "diamond", "triangle","triangle-down"，默认是"circle"
               .fillColorSet(AAColor.whiteColor)//点的填充色(用来设置折线连接点的填充色)
               .lineColorSet(@"deepskyblue")
               );
    
    
    [self.aaChartView aa_addPointsToChartSeriesArrayWithOptionsArray:@[options0]];
//    [self.aaChartView aa_onlyRefreshTheChartDataWithChartModelSeries:@[]
//                                                         animation:true];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
